package com.wwt.server.imp;

import com.wwt.server.pojo.Department;
import com.wwt.server.mapper.DepartmentMapper;
import com.wwt.server.service.IDepartmentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wwt.server.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Tao
 * @since 2021-12-05
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements IDepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;

    /**
     * 获取所有部门
     *
     * @return List
     */
    @Override
    public List<Department> getAllDepartment() {
        return departmentMapper.getAllDepartment(-1);
    }

    /**
     * 添加部门
     *
     * @param department
     * @return
     */
    @Override
    public Result addDepartment(Department department) {
        department.setEnabled(true);
        department.setIsparent(false);
        departmentMapper.addDepartment(department);
        if (department.getResult() == 1)
            return Result.success("添加成功！",department);
        return Result.fail("添加失败！");
    }

    /**
     * 删除部门
     * @param id
     * @return
     */
    @Override
    public Result deleteDepartment(Integer id) {
        Department department = new Department();
        department.setId(id);
        departmentMapper.deleteDepartment(department);
          if (department.getResult() == -1 )
              return Result.fail("部门不存在！");
          else if (department.getResult() == -2)
              return Result.fail("存在子部门，无法删除！");
          else if (department.getResult() == -3)
              return Result.fail("部门下存在员工，无法删除！");
          else
              return Result.success("删除成功！",department);

    }
}
